package com.eascs.brand.dao;

import java.util.Date;
import org.springframework.stereotype.Repository;
import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.brand.entity.O2oBrandEntity;
import com.eascs.brand.vo.BrandAndPartnerVo;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;

@Repository
public class O2oBrandDao extends BaseO2oDao {

	private static final String table_name = "o2o_brand_info";
	public static final int status_delete = 2;
	public static final int status_normal = 1;

	public BrandAndPartnerVo queryCodeAndName(String loanapplyNo) {
		
		String sql = "SELECT t.id , t.brand_id as brandId, t.partner_name as partnerName,t.partner_code as partnerCode,l.brand_name as brandName,l.brand_code as brandCode from o2o_loanapply f,o2o_partner_info t ,o2o_brand_info l where f.partner_code=t.partner_code AND t.brand_id=l.id AND f.loanapply_no=?";
		sql += "UNION SELECT t.id , t.brand_id as brandId,t.business_license_name as partnerName,t.pattern_code as partnerCode,l.brand_name as brandName,l.brand_code as brandCode from o2o_loanapply f,o2o_dealer_partner_info t ,o2o_brand_info l where f.partner_code=t.pattern_code AND t.brand_id=l.id AND f.loanapply_no=?";	
		return this.findEntity(sql, BrandAndPartnerVo.class, loanapplyNo, loanapplyNo);
	}

	public boolean insert(O2oBrandEntity entity) {
		Date d = new Date();
		entity.setCreateTime(d);
		entity.setUpdateTime(d);
		entity.setStatus(status_normal);
		entity.setWfStatus(-1);

		int result = o2oJdbcSession.insert(entity);
		return result > 0 ? true : false;
	}

	public boolean delete(String brandId) {
		String sql = "UPDATE " + table_name + " SET STATUS=? WHERE id=?";

		int result = o2oJdbcSession.execute(sql, new Object[] { status_delete, brandId });
		return result > 0 ? true : false;
	}

	public boolean update(O2oBrandEntity entity) {
		entity.setUpdateTime(new Date());

		int result = o2oJdbcSession.updateById(entity);
		return result > 0 ? true : false;
	}

	public boolean processBrandStart(String brandId) {
		String sql = "UPDATE " + table_name + " SET wf_status=? WHERE id=?";

		int result = o2oJdbcSession.execute(sql, new Object[] { ProcessTaskMain.PROECESS_STATUS_HANDING, brandId });
		return result > 0 ? true : false;
	}

	public boolean passBrandStart(String brandId) {
		String sql = "UPDATE " + table_name + " SET wf_status=? WHERE id=?";

		int result = o2oJdbcSession.execute(sql, new Object[] { ProcessTaskMain.PROECESS_STATUS_PASS, brandId });
		return result > 0 ? true : false;
	}

	public boolean updateBrandWfStatus(String brandId, int wfStatus) {
		String sql = "UPDATE " + table_name + " SET wf_status=? WHERE id=?";

		int result = o2oJdbcSession.execute(sql, new Object[] { wfStatus, brandId });
		return result > 0 ? true : false;
	}

	public O2oBrandEntity queryBrandEntityById(String brandId) {
		String sql = "SELECT * FROM " + table_name + " WHERE id=?";

		return o2oJdbcSession.findEntity(sql, O2oBrandEntity.class, brandId);
	}

}
